//
//  SMPlatFormContentViewCtrl.swift
//  SmartMachine
//
//  Created by mac on 11/23/19.
//  Copyright © 2019 Smart. All rights reserved.
//

import Cocoa

class SMPlatFormContentViewCtrl: NSSplitViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    
}

//配置源
class SMSourceListViewCtrl: NSViewController, NSOutlineViewDelegate, NSOutlineViewDataSource {
    
    @IBOutlet weak var sourceOutlineView: NSOutlineView!
    @IBOutlet weak var addAdtion: NSView!
    
    var treeRootNode : SMTreeModal! = {
           
       let rootNode = SMTreeModal.init(name: "root")
       rootNode.name = "配置信息"
       
       let node_a : SMTreeModal! = SMTreeModal.init(name: "a")
       node_a.name = "配置A"
       let node_b : SMTreeModal! = SMTreeModal.init(name: "b")
       node_b.name = "配置B"
       let node_c : SMTreeModal! = SMTreeModal.init(name: "c")
       node_c.name = "配置C"
       
       var nodes_a : NSMutableArray? = NSMutableArray.init()
       var nodes_b : NSMutableArray? = NSMutableArray.init()
       var nodes_c : NSMutableArray? = NSMutableArray.init()
       
       node_a?.childNodes = nodes_a
       node_b?.childNodes = nodes_b
       node_c?.childNodes = nodes_c
       
       var nodes_root : NSMutableArray? = NSMutableArray.init()
       nodes_root?.add(node_a!)
       nodes_root?.add(node_b!)
       nodes_root?.add(node_c!)
       rootNode.childNodes = nodes_root
       return rootNode
    } ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    
    override func viewDidAppear() {
        super.viewDidAppear()
        
        sourceOutlineView.expandItem(treeRootNode)
    }
    
    func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {

        if item != nil && item is SMTreeModal{
            return (item as! SMTreeModal).childNodes!.count
        }
        return 1
    }
    
    func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
        
        if item == nil {
            return treeRootNode!
        }
        return (item as! SMTreeModal).childNodes![index]
    }
    
    func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
        return item
    }
    
    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        
        let modal : SMTreeModal! = item as? SMTreeModal
        let rowView = SMTreeTableRowView.init()
        rowView.titleLab.stringValue = modal.name ?? ""
        
        return rowView
    }
    
    func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
        
        let modal : SMTreeModal? = item as? SMTreeModal
        if modal == nil {
            return treeRootNode!.childNodes!.count > 0
        }
        return modal!.childNodes!.count > 0
    }
    
    func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
        return 50.0
    }
    
    @IBAction func addAction(_ sender: Any) {
    }
    
    @IBAction func delAction(_ sender: Any) {
    }
}


class SMConfigSetViewCtrl: NSViewController {
    
    @IBOutlet weak var hostNameTextF: NSTextField!
    @IBOutlet weak var dbHostTextF: NSTextField!
    @IBOutlet weak var dbUserTextF: NSTextField!
    @IBOutlet weak var dbPassTextF: NSTextField!
    @IBOutlet weak var dbNameTextF: NSTextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }
    
    @IBAction func saveAction(_ sender: Any) {
        
        AppDelegate.appDelegate()?.hidePlatFormConfigCtrl()
    }
}
